package com.interview.common.result;

import com.interview.common.constant.HttpStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一API响应封装
 *
 * @param <T> 数据类型
 */
@Data
@Accessors(chain = true)
@Schema(description = "统一响应结果")
public class Result<T> implements Serializable {

    @Schema(description = "状态码", example = "200")
    private int code;

    @Schema(description = "提示信息", example = "操作成功")
    private String msg;

    @Schema(description = "响应数据")
    private T data;

    // 成功响应
    public static <T> Result<T> success() {
        return new Result<T>()
                .setCode(HttpStatus.SUCCESS)
                .setMsg("success");
    }

    // 成功响应
    public static <T> Result<T> success(T data) {
        return new Result<T>()
                .setCode(HttpStatus.SUCCESS)
                .setMsg("success")
                .setData(data);
    }

    // 成功响应（自定义消息）
    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>()
                .setCode(HttpStatus.SUCCESS)
                .setMsg(msg)
                .setData(data);
    }

    // 失败响应
    public static <T> Result<T> fail(int code, String msg) {
        return new Result<T>()
                .setCode(HttpStatus.FAIL)
                .setMsg(msg);
    }

    // 失败响应（自定义消息）
    public static <T> Result<T> fail(String msg) {
        return new Result<T>()
                .setCode(HttpStatus.FAIL)
                .setMsg(msg);
    }

    // 失败 响应（自定义消息和数据）
    public static <T> Result<T> fail(String msg, T data) {
        return new Result<T>()
                .setCode(HttpStatus.FAIL)
                .setMsg(msg)
                .setData(data);
    }
}
